﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using System.Threading;

namespace DAO
{
    public class ChiTietPhieuComDAO
    {
        public string ThemChiTietPhieuCom(ChiTietPhieuComDTO ctpcDTO)
        {
            string msg = "";
            SqlConnection conn = null;
            try
            {
                conn = DataProvider2.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "[sp_MucUngDung_ThemChiTietPhieuCom]";
                SqlParameter para;
                para = cmd.Parameters.Add("@ID_PhieuDatCom", SqlDbType.Int);
                para.Value = ctpcDTO.ID_PhieuDatCom;
                para = cmd.Parameters.Add("@ID_MonAn", SqlDbType.Int);
                para.Value = ctpcDTO.ID_MonAn;
                para = cmd.Parameters.Add("@SoLuong", SqlDbType.Int);
                para.Value = ctpcDTO.SoLuong;
                para = cmd.Parameters.Add("@Gia", SqlDbType.Int);
                para.Value = ctpcDTO.Gia;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return msg;
        }

        public string ThemChiTietPhieuCom2(DataGridView dtg)
        {
            string msg = "";
            SqlConnection conn = null;
            try
            {
                conn = DataProvider2.ConnectDB();
                for (int i = 0; i < dtg.Rows.Count; i++)
                {
                    SqlCommand cmd = new SqlCommand("INSERT INTO ChiTietPhieuCom(ID_PhieuDatCom,ID_MonAn,SoLuong,Gia) values (@ID_PhieuDatCom,@ID_MonAn,@SoLuong,@Gia)", conn);
                    cmd.Parameters.AddWithValue("@ID_PhieuDatCom", dtg.Rows[i].Cells[0].Value);
                    cmd.Parameters.AddWithValue("@ID_MonAn", dtg.Rows[i].Cells[1].Value);
                    cmd.Parameters.AddWithValue("@SoLuong", dtg.Rows[i].Cells[3].Value);
                    cmd.Parameters.AddWithValue("@Gia", dtg.Rows[i].Cells[4].Value);
                    cmd.ExecuteNonQuery();
                }
                conn.Close();
                MessageBox.Show("Thêm chi tiết phiếu thành công!");
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            return msg;
        }

        public string ThemChiTietPhieuCom2(DataGridView dtg, int chedo)
        {
            string msg = "";
            SqlConnection conn = null;
            try
            {
                conn = DataProvider2.ConnectDB(chedo);
                for (int i = 0; i < dtg.Rows.Count; i++)
                {
                    SqlCommand cmd = new SqlCommand("INSERT INTO ChiTietPhieuCom(ID_PhieuDatCom,ID_MonAn,SoLuong,Gia) values (@ID_PhieuDatCom,@ID_MonAn,@SoLuong,@Gia)", conn);
                    cmd.Parameters.AddWithValue("@ID_PhieuDatCom", dtg.Rows[i].Cells[0].Value);
                    cmd.Parameters.AddWithValue("@ID_MonAn", dtg.Rows[i].Cells[1].Value);
                    cmd.Parameters.AddWithValue("@SoLuong", dtg.Rows[i].Cells[3].Value);
                    cmd.Parameters.AddWithValue("@Gia", dtg.Rows[i].Cells[4].Value);
                    cmd.ExecuteNonQuery();
                }
                conn.Close();
                MessageBox.Show("Thêm chi tiết phiếu thành công!");
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            return msg;
        }

        public List<ChiTietPhieuComDTO> LayDanhSachCPPCTheoMa(int maPDC)
        {
            List<ChiTietPhieuComDTO> listCTPC = new List<ChiTietPhieuComDTO>();
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                String sql = "Select * from ChiTietPhieuCom Where ID_PhieuDatCom = " + maPDC;

                conn = DataProvider2.ConnectDB();
                SqlCommand cm = new SqlCommand(sql, conn);
                reader = cm.ExecuteReader();
                while (reader.Read())
                {
                    ChiTietPhieuComDTO ctpcDTO = new ChiTietPhieuComDTO();
                    ctpcDTO.ID_CT_PhieuCom = reader.GetInt32(0);
                    ctpcDTO.ID_PhieuDatCom = reader.GetInt32(1);
                    ctpcDTO.ID_MonAn = reader.GetInt32(2);
                    ctpcDTO.SoLuong = reader.GetInt32(3);
                    ctpcDTO.Gia = reader.GetInt32(4);

                    listCTPC.Add(ctpcDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return listCTPC;
        }
    }
}
